from django.db import models
from django.core import validators

import myValidators

def returnWhen(fact1, fact2, fact3, logical_operator1, logical_operator2):
    result_string = str(fact1)
    if not(fact2 == None):
        result_string += " " + str(logical_operator1) + " " + str(fact2)
        if not(fact3 == None):
            result_string += " " + str(logical_operator2) + " " + str(fact3)
            
    return result_string

def returnRule(fact1, fact2, fact3, logical_operator1, logical_operator2, rec1, rec2):
    result_string = "if " + returnWhen(fact1, fact2, fact3, logical_operator1, logical_operator2) + " :  " + str(rec1)
    if not(rec2 == None):
        result_string += " " + str(rec2)
    
    return result_string



class Fact(models.Model):
    name = models.CharField(max_length=30, validators=[myValidators.validate_facts_and_recs])
        
    def clean(self):
        if not(self.name.find(" ")>=0):
            if not(self.name.find("not_")==0):
                Fact(name="not_"+self.name).save()
    
    def __unicode__(self):
        return self.name

class Question(models.Model):
    name = models.CharField(max_length=30)
    fact = models.ForeignKey(Fact)
    question = models.TextField(max_length=300)
    
    def __unicode__(self):
        return self.name
    
#choice for logical operators
logical_operator_choices = (('or', 'OR'), ('and', 'AND'))
    
class QuestionSet(models.Model):
    
    fact1 = models.ForeignKey(Fact, related_name="QS_fact1", null=True, blank=True)
    fact2 = models.ForeignKey(Fact, related_name="QS_fact2", null=True, blank=True)
    fact3 = models.ForeignKey(Fact, related_name="QS_fact3", blank=True, null=True)
    logical_operator1 = models.CharField(max_length=3, blank=True, null=True, choices=logical_operator_choices)
    logical_operator2 = models.CharField(max_length=3, blank=True, null=True, choices=logical_operator_choices)
    name = models.CharField(max_length=30)
    question_sets_for_questions = models.ManyToManyField(Question, blank=True, null=True)
    question_sets_for_question_sets = models.ManyToManyField('self', blank=True, null=True, symmetrical=False)
     
    def __unicode__(self):
        return self.name

class QuestionSetList(models.Model):
    name = models.CharField(max_length=50)
    question_sets_list = models.ManyToManyField(QuestionSet)
    
    def __unicode__(self):
        return self.name
    
class Rec(models.Model):
    name = models.CharField(max_length=30, validators=[myValidators.validate_facts_and_recs])
    
    def __unicode__(self):
        return self.name
    
class Rule(models.Model):
    
    fact1 = models.ForeignKey(Fact, related_name="Rule_fact1")
    fact2 = models.ForeignKey(Fact, related_name="Rule_fact2", blank=True, null=True)
    fact3 = models.ForeignKey(Fact, related_name="Rule_fact3", blank=True, null=True)
    logical_operator1 = models.CharField(max_length=3, blank=True, null=True, choices=logical_operator_choices)
    logical_operator2 = models.CharField(max_length=3, blank=True, null=True, choices=logical_operator_choices)
    rec1 = models.ForeignKey(Rec, related_name="Rule_rec1")
    rec2 = models.ForeignKey(Rec, related_name="Rule_rec2", blank=True, null=True)
    
    def clean(self):
        myValidators.validate_rule(self.rec1, self.rec2)
        myValidators.validate_when(self.fact1, self.fact2, self.fact3, self.logical_operator1, self.logical_operator2)

    def __unicode__(self):
        return returnRule(self.fact1, self.fact2, self.fact3, self.logical_operator1, self.logical_operator2, self.rec1, self.rec2)

    
class Explanation(models.Model):
    
    rec =  models.ForeignKey(Rec, related_name="Exp_rec")
    human_readable_form = models.TextField(max_length=100)
    detailed_description = models.TextField(max_length=500)
    
    def __unicode__(self):
        return self.human_readable_form
